﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using BLL.Entities.Company;
using BLL.Entities;

namespace HTAProDataImporter.Transport
{
    public class CourtTransport : BaseEntityTransport
    {
        public override string CommandText
        {
            get { return "select * from tblCourt"; }
        }

        public override void InstanceFromReader(OleDbDataReader DR)
        {

                CourtLocation court = CourtLocation.CreateNewInstance<CourtLocation>();

                court.Name = DR["CourtName"].ToString();
                court.Address.AddressLine = DR["Address1"].ToString();
                if (!string.IsNullOrEmpty(DR["Address2"].ToString()))
                    court.Address.AddressLine = string.Format("{0}, {1}", court.Address.AddressLine, DR["Address2"].ToString());

                court.Address.City = DR["City"].ToString();
                court.Address.Province = DR["fkProvinceID"].ToString();
                court.Address.PostalCode = DR["PostalCode"].ToString();

                court.Contact.Fax = DR["Fax"].ToString();
                court.Contact.PhoneWork = DR["Phone"].ToString();
                if (!string.IsNullOrEmpty(DR["ext"].ToString()))
                    court.Contact.PhoneWork = string.Format("{0} ext: {1}", court.Contact.PhoneWork, DR["ext"]);

                court.Region = DR["Region"].ToString();



                if (!string.IsNullOrEmpty(DR["fkIconID"].ToString()))
                    try
                    {
                        court.ICON = DataStore.ICON[DR["fkIconID"].ToString()];
                    }
                    catch (Exception ex)
                    {
                        Warnings.Add(string.Format("Can't find Officer ICON for key IconID {0}, OfficerID {1}", DR["fkIconID"], DR["pkCourtID"]));
                    }
                else
                    Warnings.Add(string.Format("Officer has no ICON OfficerID {0}", DR["pkCourtID"]));


                CourtProsecutor pros1 = CourtProsecutor.CreateNewInstance<CourtProsecutor>();
                pros1.CourtID = court.EntityID;
                pros1.CaseType = DataStore.CourtProsecutorType.Values["Part 1 (Municipal) Prosecutor"];

                pros1.Address = AddressInfo.CreateNewInstance<AddressInfo>();
                pros1.Contact = ContactInfo.CreateNewInstance<ContactInfo>();
                pros1.Person = PersonInfo.CreateNewInstance<PersonInfo>();

                pros1.Address.AddressLine = DR["Part1Address"].ToString();
                pros1.Address.City = DR["Part1City"].ToString();
                pros1.Address.Province = DR["Part1Province"].ToString();
                pros1.Address.PostalCode = DR["Part1Postal"].ToString();

                pros1.Contact.Fax = DR["Part1Fax"].ToString();
                pros1.Contact.PhoneWork = DR["Part1Phone"].ToString();
                if (!string.IsNullOrEmpty(DR["Part1Ext"].ToString()))
                    pros1.Contact.PhoneWork = string.Format("{0} ext: {1}", pros1.Contact.PhoneWork, DR["Part1Ext"]);

                court.Prosecutors.Add(pros1);


                CourtProsecutor pros2 = CourtProsecutor.CreateNewInstance<CourtProsecutor>();
                pros2.CourtID = court.EntityID;
                pros2.CaseType = DataStore.CourtProsecutorType.Values["Part 3 (Provincial) Prosecutor"];

                pros2.Address = AddressInfo.CreateNewInstance<AddressInfo>();
                pros2.Contact = ContactInfo.CreateNewInstance<ContactInfo>();
                pros2.Person = PersonInfo.CreateNewInstance<PersonInfo>();

                pros2.Address.AddressLine = DR["Part3Address"].ToString();
                pros2.Address.City = DR["Part3City"].ToString();
                pros2.Address.Province = DR["Part3Province"].ToString();
                pros2.Address.PostalCode = DR["Part3Postal"].ToString();

                pros2.Contact.Fax = DR["Part3Fax"].ToString();
                pros2.Contact.PhoneWork = DR["Part3Phone"].ToString();
                if (!string.IsNullOrEmpty(DR["Part3Ext"].ToString()))
                    pros2.Contact.PhoneWork = string.Format("{0} ext: {1}", pros2.Contact.PhoneWork, DR["Part3Ext"]);

                court.Prosecutors.Add(pros2);


                court.AppealCourtAddress.AddressLine = DR["AppealAddress1"].ToString();
                if (!string.IsNullOrEmpty(DR["AppealAddress2"].ToString()))
                    court.AppealCourtAddress.AddressLine = string.Format("{0}, {1}", court.AppealCourtAddress.AddressLine, DR["AppealAddress2"].ToString());

                court.AppealCourtAddress.City = DR["AppealCity"].ToString();
                court.AppealCourtAddress.Province = DR["AppealfkProvinceID"].ToString();
                court.AppealCourtAddress.PostalCode = DR["AppealPostalCode"].ToString();


                court.AppealCourtContact.Fax = DR["AppealFax"].ToString();
                court.AppealCourtContact.PhoneWork = DR["AppealPhone"].ToString();
                if (!string.IsNullOrEmpty(DR["Appealext"].ToString()))
                    court.AppealCourtContact.PhoneWork = string.Format("{0} ext: {1}", court.Contact.PhoneWork, DR["Appealext"]);



                DataStore.Courts.Add(DR["pkCourtID"].ToString(), court);
           
        }
    }
}
